use crate::dao::DataItemDao;
use crate::models::DataItem;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;

pub struct DataItemService {
    data_item_dao: DataItemDao,
}

impl DataItemService {
    pub fn new(pool: Pool<SqliteConnectionManager>) -> Self {
        let data_item_dao = DataItemDao::new(pool);
        DataItemService { data_item_dao }
    }

    pub fn init_table(&self) -> Result<(), rusqlite::Error> {
        self.data_item_dao.create_table()
    }

    pub fn insert_data_item(&self, data_item: &DataItem) -> Result<(), rusqlite::Error> {
        self.data_item_dao
            .insert(data_item)
            .expect("Failed to insert data item");
        Ok(())
    }

    pub async fn get_all_data_items(&self) -> Result<Vec<DataItem>, rusqlite::Error> {
        self.data_item_dao.get_all_data_items()
    }
}
